package PAT;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class PAT2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Map<Integer, Float> map = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        int a = scanner.nextInt();
        while (a-->0){
            int i = scanner.nextInt();
            float f = scanner.nextFloat();
            map.put(i,f);
        }
        a = scanner.nextInt();
        while (a-->0){
            int i = scanner.nextInt();
            float f = scanner.nextFloat();
            if(map.containsKey(i)){
                map.put(i,map.get(i)+f);
            }else{
                map.put(i,f);
            }
        }
        System.out.print(map.size()+" ");
        int p =0;
        for (Map.Entry<Integer, Float> entry : map.entrySet()) {
            if(++p<map.size()){
                System.out.print(entry.getKey()+" ");
                System.out.print(entry.getValue()+" ");
            }else{
                System.out.print(entry.getKey()+" ");
                System.out.print(entry.getValue());
            }
        }
    }
}
